package actions;

import gui.Frame;
import information.Date;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JTextField;

/**
 * An action for a park manager to submit a new job. 
 * 
 * @author Natalie
 *
 */
@SuppressWarnings("serial")
public class SubmitJobAction extends AbstractAction {
	
	private final Frame myFrame;

	/**
	 * The combo box the manager can choose a park from. 
	 */
	private final JComboBox myPark;
	/**
	 * The area a manager will type the description of their job in. 
	 */
	private final JTextField myDescription;
	/**
	 * The field the manager will enter their start date in. The date must be in the correct format. 
	 */
	private final JTextField myStartDate;
	/**
	 * The field the manager will enter their end date in. The date must be in the correct format
	 */
	private final JTextField myEndDate;
	/**
	 * Will be checked if the job is on a weekday(m-f). 
	 */
	private final JCheckBox myWeekday;
	/**
	 * Will be checked if the job is on a holiday. 
	 */
	private final JCheckBox myHoliday; 
	/**
	 * The field where the manager will enter the number of light volunteers they need. 
	 */
	private final JTextField myLightNumber;
	/**
	 * The field where the manager will enter the description of light work. 
	 */
	private final JTextField myLightDescription;
	/**
	 * The field where the manager will enter the number of medium volunteers they need. 
	 */
	private final JTextField myMediumNumber;
	/**
	 * The field where the manager will enter the medium work description.
	 */
	private final JTextField myMediumDescription;
	/**
	 * The field where the manager will enter the number of heavy volunteers they need. 
	 */
	private final JTextField myHeavyNumber;
	/**
	 * The field where the manager will enter the heavy work description. 
	 */
	private final JTextField myHeavyDescription;
	
	/**
	 * Creates a new action to submit a job. 
	 * 
	 * @param theFrame The frame the action will be performed on. 
	 * @param thePark The job's park field. 
	 * @param theDescription The job's description field. 
	 * @param theStartDate The job's start date field. 
	 * @param theEndDate The job's end date field. 
	 * @param theWeekday The job's weekday field. 
	 * @param theHoliday The job's holiday field. 
	 * @param theLightNumber The job's field for the number of light volunteers. 
	 * @param theLightDescription The job's field for the light work description. 
	 * @param theMediumNumber The job's field for the medium number of volunteers needed. 
	 * @param theMediumDescription The job's field for the medium work description. 
	 * @param theHeavyNumber The job's field for the heavy number of volunteers needed. 
	 * @param theHeavyDescription The job's field for the heavy work description. 
	 */
	public SubmitJobAction(final Frame theFrame, final JComboBox thePark, final JTextField theDescription, 
			final JTextField theStartDate, final JTextField theEndDate, final JCheckBox theWeekday, final JCheckBox theHoliday, 
			final JTextField theLightNumber, final JTextField theLightDescription, final JTextField theMediumNumber, final JTextField theMediumDescription,
			final JTextField theHeavyNumber, final JTextField theHeavyDescription) {
		super("Submit Job");
		myFrame = theFrame;
		myPark = thePark;
		myDescription = theDescription;
		myStartDate = theStartDate;
		myEndDate = theEndDate;
		myWeekday = theWeekday;
		myHoliday = theHoliday; 
		myLightNumber = theLightNumber;
		myLightDescription = theLightDescription;
		myMediumNumber = theMediumNumber;
		myMediumDescription = theMediumDescription;
		myHeavyNumber = theHeavyNumber;
		myHeavyDescription = theHeavyDescription;
		
		
	}
	
	/**
	 * Creates a new job based on all of the information that the park manager entered. 
	 * Will display a success message if the job is successfully entered. 
	 */
	@Override
	public void actionPerformed(final ActionEvent theEvent) {
		myFrame.setPreviousAction(new CreateJobAction(myFrame));
		List<String> levelDetails = new ArrayList<String>();
		levelDetails.add(myLightDescription.getText());
		levelDetails.add(myMediumDescription.getText());
		levelDetails.add(myHeavyDescription.getText());
		
		List<Integer> levelNumbers = new ArrayList<Integer>();
		levelNumbers.add(Integer.parseInt(myLightNumber.getText()));
		levelNumbers.add(Integer.parseInt(myMediumNumber.getText()));
		levelNumbers.add(Integer.parseInt(myHeavyNumber.getText()));
		
		String[] startDate = myStartDate.getText().split(" ");
		String[] endDate = myEndDate.getText().split(" ");
		
		if(myFrame.getData().createNewJob(myFrame.getData().getCurrentUser(), levelDetails, levelNumbers, 
				new Date(Integer.parseInt(startDate[2]), Integer.parseInt(startDate[0]), Integer.parseInt(startDate[1]), 
						Integer.parseInt(startDate[3]), Integer.parseInt(startDate[4])), 
						new Date(Integer.parseInt(endDate[2]), Integer.parseInt(endDate[0]), Integer.parseInt(endDate[1]), 
								Integer.parseInt(endDate[3]), Integer.parseInt(endDate[4])), myDescription.getText(),
								myWeekday.isSelected(), myHoliday.isSelected(), myFrame.getData().getCurrentUser().getPark((String) myPark.getSelectedItem()))) {
				myFrame.displaySuccess("Your job has been submitted. Thank you!");
		}
	}

}
